﻿using RpcClient;
using RpcHelper;
using RpcManageClient.Model;
using RpcModel;
using SqlExecHelper;
using Wedonek.RpcStore.Service.DAL;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.LocalEvent;
using Wedonek.RpcStore.Service.Model;
namespace Wedonek.RpcStore.Service.Collect
{

    internal class ServerCollect : BasicCollect<RemoteServerConfigDAL>, IServerCollect
    {
        public bool CheckIsExists(long sysTypeId)
        {
            return this.BasicDAL.CheckIsExists(sysTypeId);
        }

        public void SetState(ServerConfigDatum config, RpcServiceState state)
        {
            this.BasicDAL.SetState(config.Id, state);
            config.ServiceState = state;
            new ServerEvent("SetState")
            {
                Datum = config.ConvertMap<ServerConfigDatum, ServiceDatum>()
            }.AsyncPublic();
        }
        public long Add(ServerConfigAddParam add)
        {
            add.ServerIndex = this._GetServiceIndex(add.SystemType, add.ServerMac);
            return this.BasicDAL.Add(add);
        }
        private int _GetServiceIndex(long sysTypeId, string mac)
        {
            return this.BasicDAL.GetServiceIndex(sysTypeId, mac);
        }
        public void Set(ServerConfigDatum config, ServerConfigSetParam set)
        {
            if (set.ServerPort != config.ServerPort || set.ServerMac != config.ServerMac)
            {
                this.CheckServerPort(set.ServerMac, set.ServerPort);
            }
            this.BasicDAL.SetService(config.Id, set);
            config = config.ConvertInto(set);
            new ServerEvent("Set")
            {
                Datum = config.ConvertMap<ServerConfigDatum, ServiceDatum>()
            }.AsyncPublic();
        }

        public void Delete(ServerConfigDatum server)
        {
            RemoteServerGroupDAL groupDAL = new RemoteServerGroupDAL();
            ContainerDAL containerDAL = new ContainerDAL();
            using (ITransaction tran = new LocalTransaction())
            {
                groupDAL.Clear(server.Id);
                if (server.IsContainer)
                {
                    containerDAL.Delete(server.ContainerId.Value);
                }
                this.BasicDAL.Delete(server.Id);
                tran.Commit();
            }
            new ServerEvent("Delete")
            {
                Datum = server.ConvertMap<ServerConfigDatum, ServiceDatum>()
            }.AsyncPublic();
        }
        public ServerConfigDatum[] Query(QueryServiceParam query, IBasicPage paging, out long count)
        {
            return this.BasicDAL.Query(query, paging, out count);
        }
        public ServerConfigDatum[] Gets(long[] ids)
        {
            if (ids.Length == 0)
            {
                return new ServerConfigDatum[0];
            }
            return this.BasicDAL.GetServices(ids);
        }
        public ServerConfigDatum Get(long id)
        {
            ServerConfigDatum config = this.BasicDAL.Get(id);
            if (config == null)
            {
                throw new ErrorException("rpc.server.not.find");
            }
            return config;
        }

        public bool CheckIsOnline(long id)
        {
            return this.BasicDAL.CheckIsOnline(id);
        }

        public void CheckServerPort(string mac, int serverPort)
        {
            if (this.BasicDAL.CheckServerPort(mac, serverPort))
            {
                throw new ErrorException("rpc.server.port.repeat");
            }
        }

        public bool CheckIsExistsByGroup(long groupId)
        {
            return this.BasicDAL.CheckIsExistsByGroup(groupId);
        }

        public bool CheckRegion(int regionId)
        {
            return this.BasicDAL.CheckRegion(regionId);
        }

        public BasicService[] GetBasic(long[] ids)
        {
            if (ids.Length == 0)
            {
                return System.Array.Empty<BasicService>();
            }
            return this.BasicDAL.GetBasic(ids);
        }
    }
}
